/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.Server.L1World.WorldClan;
import l1j.opqlo.Server.L1World.WorldWar;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.CharacterTable;
import l1j.server.server.datatables.lock.ClanEmblemReading;
import l1j.server.server.datatables.lock.ClanReading;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.L1War;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_CharTitle;
import l1j.server.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

public class C_LeaveClan extends ClientBasePacket {

	private static final String C_LEAVE_CLAN = "[C] C_LeaveClan";
	private static Logger _log = Logger.getLogger(C_LeaveClan.class.getName());

	@Override
	public String getType() {
		return C_LEAVE_CLAN;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);
			final L1PcInstance player = client.getActiveChar();
			if (player == null) {
				return;
			}
			final String player_name = player.getName();
			final String clan_name = player.getClanname();
			final int clan_id = player.getClanid();
			if (clan_id == 0) {// クラン未所属
				return;
			}

			final L1Clan clan = WorldClan.get().getClan(clan_name);
			if (clan != null) {
				final String clan_member_name[] = clan.getAllMembers();
				int i;
				if (player.isCrown() && player.getId() == clan.getLeaderId()) { // プリンスまたはプリンセス、かつ、血盟主
					final int castleId = clan.getCastleId();
					final int houseId = clan.getHouseId();
					if (castleId != 0 || houseId != 0) {
						player.sendPackets(new S_ServerMessage(665)); // \f1城やアジトを所有した状態で血盟を解散することはできません。
						return;
					}
					for (final L1War war : WorldWar.get().getWarList()) {
						if (war.checkClanInWar(clan_name)) {
							player.sendPackets(new S_ServerMessage(302)); // \f1解散させることができません。
							return;
						}
					}

					for (i = 0; i < clan_member_name.length; i++) { // クラン員のクラン情報をクリア
						final L1PcInstance online_pc = L1World.getInstance().getPlayer(
								clan_member_name[i]);
						if (online_pc != null) { // オンライン中のクラン員
							online_pc.setClanid(0);
							online_pc.setClanname("");
							online_pc.setClanRank(0);
							online_pc.setTitle("");
							online_pc.sendPackets(new S_CharTitle(
									online_pc.getId(), ""));
							online_pc.broadcastPacketAll(new S_CharTitle(online_pc
									.getId(), ""));
							online_pc.save(); // DBにキャラクター情報を書き込む
							online_pc.sendPackets(new S_ServerMessage(269,
									player_name, clan_name)); // %1血盟の血盟主%0が血盟を解散させました。
						} else { // オフライン中のクラン員
							try {
								final L1PcInstance offline_pc = CharacterTable.getInstance().restoreCharacter(clan_member_name[i]);
								offline_pc.setClanid(0);
								offline_pc.setClanname("");
								offline_pc.setClanRank(0);
								offline_pc.setTitle("");
								offline_pc.save(); // DBにキャラクター情報を書き込む
							} catch (final Exception e) {
								_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
							}
						}
					}
					ClanEmblemReading.get().deleteIcon(clan_id);
//					String emblem_file = String.valueOf(clan_id);
//					File file = new File("emblem/" + emblem_file);
//					file.delete();
					ClanReading.get().deleteClan(clan_name);
				} else { // 血盟主以外
					final L1PcInstance clanMember[] = clan.getOnlineClanMember();
					for (i = 0; i < clanMember.length; i++) {
						clanMember[i].sendPackets(new S_ServerMessage(178,player_name, clan_name)); // \f1%0が%1血盟を脱退しました。
					}
					if (clan.getWarehouseUsingChar() == player.getId()){ // 自キャラがクラン倉庫使用中

						clan.setWarehouseUsingChar(0); // クラン倉庫のロックを解除
					}
					player.setClanid(0);
					player.setClanname("");
					player.setClanRank(0);
					player.setTitle("");
					player.sendPackets(new S_CharTitle(player.getId(), ""));
					player.broadcastPacketAll(new S_CharTitle(player.getId(), ""));
					player.save(); // DBにキャラクター情報を書き込む
					clan.delMemberName(player_name);
				}
			} else {
				player.setClanid(0);
				player.setClanname("");
				player.setClanRank(0);
				player.setTitle("");
				player.sendPackets(new S_CharTitle(player.getId(), ""));
				player.broadcastPacketAll(new S_CharTitle(player.getId(), ""));
				player.save(); // DBにキャラクター情報を書き込む
				player.sendPackets(new S_ServerMessage(178, player_name, clan_name)); // \f1%0が%1血盟を脱退しました。
			}

		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
